﻿using API;
using API.Sys;
using Common;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class UserBLL
    {
        public bool HeartBeat(out string msg) 
        {
            msg = string.Empty;
            try
            {
                ApiSystem dataProxy = ServiceProxy.ApiSystemProxy();
                DateTime serverTime = dataProxy.HeartBeat();
                TimeSpan ts = serverTime - DateTime.Now;
                if (Math.Abs(ts.Days) > 0) 
                {
                    msg = "销售POS端本地时间设置错误,请重新设置计算的日期和时间！";
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("UserBLL.HeartBeat", ex);
                return false;
            }
        }
        public void BatchInsert()
        {
            try
            {
                ApiSystem dataProxy = ServiceProxy.ApiSystemProxy();
                MS_User[] users = dataProxy.GetUsers(null);
                if (users == null || users.Length <= 0) return;
                using (UnitOfWork uow = DatabaseSchema.UnitOfWork)
                {
                    foreach (MS_User u in users)
                    {
                        S_User user = new S_User(uow);
                        user.UserID =u.UserID;
                        user.UserName =u.UserName;
                        user.UserNo = u.UserNo;
                        user.LoginName = u.LoginName;
                        user.Password = u.Password;
                        user.Department = u.Department;
                        user.AdminFlagID = u.AdminFlagID;
                        user.StatusID = u.StatusID;
                        user.Creator = u.Creator;
                        user.CreateDT = u.CreateDT;
                        user.Operator = u.Operator;
                        user.UpdateDT =u.UpdateDT.Value;
                        user.Remark = u.Remark;
                        user.Save();
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("UserBLL.BatchInsert", ex);
            }
        }
    }
}
